package dts

import (
	lsql "jujiu/data/persist/mysql"
	"log"
)




func Syn2OrtherDatasource(deviceId int64, src string, target string) (bool, string) {
	sourceClinet, exist := lsql.DataSourceClientMap[src]
	info := &lsql.DeviceBaseInfo{}
	if !exist {
		log.Println("获取源数据源失败！")
		return false, "获取源数据源失败"
	}
	mapper := lsql.NewDeviceBaseMapper(&sourceClinet)
	info = mapper.QueryDeviceBaseInfoByDeviceId(deviceId)
	if info == nil {
		log.Println("设备不存在，同步失败！", deviceId, src, target)
		return false, "设备不存在，同步失败！"
	}
	targetClient, ok := lsql.DataSourceClientMap[target]
	if !ok {
		log.Println("获取目标数据源失败！")
		return false, "获取目标数据源失败！"
	}
	targetmapper := lsql.NewDeviceBaseMapper(&targetClient)
	trinfo := targetmapper.QueryDeviceBaseInfoByDeviceId(deviceId)
	if trinfo == nil {
		// insert
		insertNum := targetmapper.InsertDeviceBaseInfo(*info)
		if insertNum < 1 {
			return false, "Insert失败！"
		}
	} else {
		updateNum := targetmapper.UpdateDeviceBaseInfo(*info)
		if updateNum < 1 {
			return false, "更新失败！"
		}
	}

	return true, "SUCCESS"
}

func ResetDeviceCid2CNReal(deviceId int64, tcid string) (bool, string) {
	clinet, ok := lsql.DataSourceClientMap["cn_real"]
	if !ok {
		log.Println("获取数据源失败！")
		return false, "获取数据源失败"
	}
	cmapper := lsql.NewDeviceBaseMapper(&clinet)
	deviceBaseInfo := cmapper.QueryDeviceBaseInfoByDeviceId(deviceId)
	if deviceBaseInfo == nil {
		log.Println("设备不存在", deviceId)
		return false, "设备不存在！"
	}
	deviceBaseInfo.TCid = tcid
	info := cmapper.UpdateDeviceBaseInfo(*deviceBaseInfo)
	if info < 1 {
		return false, "更新失败！"
	}
	return true, "SUCCESS"
}
